﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="类型" prop="Type">
          <a-select v-model="entity.Type" placeholder="类型" style="width: 100%">
            <a-select-option v-for="item in List" :key="item.Id" :value="item.DicValue">
              {{ item.DicName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="规则" prop="Rule">
          <a-input v-model="entity.Rule" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="排序" prop="Sort">
          <a-input-number v-model="entity.Sort" :style="{width:'100%'}" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="长度" prop="length">
          <a-input-number v-model="entity.length" @change="handleInputChange" :style="{width:'100%'}" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      List: [],
      rules: {
        Type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        Sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        length: [{ required: true, message: '请输入长度', trigger: 'blur' }]
      },
      title: '',
      allLength: 0,
      CodeLength: 0,
      nowLength: 0,
      edit: false
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (typeId, id, title, length, CodeLength) {
      this.init()
      this.title = title
      this.entity.TypeId = typeId
      this.allLength = length
      this.CodeLength = CodeLength
      this.getSelect()
      if (id) {
        this.loading = true
        this.edit = true
        this.$http.post('/Base_Manage/Base_BarCodeRule/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
          this.nowLength = resJson.Data.length
        })
      }
    },
    getSelect () {
      this.$http
        .post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
          Condition: 'DicKey',
          Keyword: 'BarCodeRuleType'
        })
        .then(res => {
          if (res.Success) {
            this.List = res.Data.DictionaryDetailsList
          }
        })
    },
    handleInputChange (value) {
      this.entity.length = parseInt(value, 10)
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        if (this.entity.length <= 0) {
          this.$message.error('分项编码规则长度不能小于等于0')
          return
        }
        if ((this.edit && this.allLength - this.nowLength + this.entity.length > this.CodeLength) || (!this.edit && this.allLength + this.entity.length > this.CodeLength)) {
          this.$message.error('分项编码规则长度合计不能大于编码长度')
          return
        }
        this.loading = true
        this.$http.post('/Base_Manage/Base_BarCodeRule/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
